﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Rotempco.ORM.Generator.Logic;
using Rotempco.ORM.Generator.Objects;
using System.IO;
using Rotempco.ORM.Generator.Enums;
using Rotempco.ORM.Generator.Managers;
using Microsoft.Win32;

namespace Rotempco.ORM.Generator
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button_BrowseForSolutionFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "VS Solution files|*.sln";
            if (openFileDialog.ShowDialog() == true)
            {
                textBox_SolutionName.Text = openFileDialog.FileName;
                string mappingsFile = GetMappingsFile();
                if (File.Exists(mappingsFile))
                {
                    LoadSettings(mappingsFile);
                }
            }
        }

        private void LoadSettings(string mappingsFile)
        {
            RotempcoSettings projectSettings = RotempcoSettings.GetProjectSettings(mappingsFile);

            textBox_BaseProjectFile.Text = projectSettings.GetValue("@MainProjectFile");
            textBox_ProjectName.Text = projectSettings.GetValue("@ProjectName");
            textBox_DBIP.Text = projectSettings.GetValue("@DatabaseAddress");
            textBox_DBName.Text = projectSettings.GetValue("@DatabaseName");
            textBox_DBLogin.Text = projectSettings.GetValue("@DatabaseLogin");
            textBox_DBPassword.Text = projectSettings.GetValue("@DatabasePassword");
            checkBox_DBUseInegrated.IsChecked = projectSettings.GetBoolValue("@DatabaseUseIntegrated");
            
        }

        private void button_BeowseForBaseProjectFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "VS project files|*.csproj";
            if (openFileDialog.ShowDialog() == true)
            {
                textBox_BaseProjectFile.Text = openFileDialog.FileName;
            }
        }

        private void button_LoadTables_Click(object sender, RoutedEventArgs e)
        {
            string mappingsFile = GetMappingsFile();
            RotempcoSettings projectSettings = RotempcoSettings.GetProjectSettings(mappingsFile);

            string baseProjectFile = textBox_BaseProjectFile.Text;
            baseProjectFile = baseProjectFile.Replace(new FileInfo(mappingsFile).Directory.FullName, "");

            string projectName = textBox_ProjectName.Text;

            projectSettings.BundleUpdate(() =>
            {
                projectSettings.SetKey("@MainProjectFile", baseProjectFile);

                projectSettings.SetKey("@ProjectName", projectName);

                projectSettings.SetKey("@EntityObjectNameSpace", projectName + ".BaseClass.Objects.EntityObjects");
                projectSettings.SetKey("@PropertyEntityObjectTemplateNameSpace", projectName + ".BaseClass.Objects.EntityObjects.PropertyEntities");
    
                projectSettings.SetKey("@DALImplementationNamespace", projectName + ".BaseClass.DAL");
                projectSettings.SetKey("@ORMCoreObjects", projectName + ".Core.ORM.Objects");
                projectSettings.SetKey("@ORMLogic", projectName + ".Core.ORM.Logic");
                projectSettings.SetKey("@CoreLogic", projectName + ".Core.Logic");

                projectSettings.SetKey("@FactoryPath", @"\" + projectName + @".BaseClass\");
                projectSettings.SetKey("@EntityObjectPath", @"\" + projectName + @".BaseClass\Objects\EntityObjects\");
                projectSettings.SetKey("@PropertyEntityObject", @"\" + projectName + @".BaseClass\Objects\EntityObjects\PropertyEntities\");

                projectSettings.SetKey("@DALImplementationPath", @"\" + projectName + @".BaseClass\DAL\");

                projectSettings.SetKey("@DatabaseAddress", textBox_DBIP.Text);
                projectSettings.SetKey("@DatabaseName", textBox_DBName.Text);
                projectSettings.SetKey("@DatabaseLogin", textBox_DBLogin.Text);
                projectSettings.SetKey("@DatabasePassword", textBox_DBPassword.Text);
                projectSettings.SetKey("@DatabaseUseIntegrated", checkBox_DBUseInegrated.IsChecked);
            });

            TableView tableView = new TableView();
            this.Content = tableView;
            tableView.SetSettings(projectSettings);
        }

        private string GetMappingsFile()
        {
            try
            {
                FileInfo fileInfo = new FileInfo(textBox_SolutionName.Text);

                string mappingsFile = fileInfo.FullName.Substring(0, fileInfo.FullName.LastIndexOf('.')) + ".mapping";
                return mappingsFile;
            }
            catch (Exception ee)
            {
                throw new Exception("Could not load mapping file");
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string mappingsFile = GetMappingsFile();
                RotempcoSettings projectSettings = RotempcoSettings.GetProjectSettings(mappingsFile);

                FileInfo projectFile = new FileInfo(projectSettings.SettingsFile);

                string projectPath = projectFile.Directory.FullName;
                ORMManager.Instance.SetupDirectoryStructure(projectSettings, projectPath);

                MessageBox.Show("File structure created. You must include new folders in project yourself");
            }
            catch (Exception ee)
            {
                MessageBox.Show("Fejl: " + ee.Message);
            }
        }


    }
}
